package com.ynet.hmds28.movie.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.boot.hsf.annotation.HSFProvider;
import com.ynet.hm.movie.domain.MovieBean;
import com.ynet.hm.movie.domain.MovieListBean;
import com.ynet.hm.movie.service.QueryMovieService;
import com.ynet.hm.utils.UuidUtil;
import com.ynet.hmds28.movie.dao.MovieDao;
import com.ynet.hmds28.movie.dao.MovieflowDao;
import com.ynet.hmds28.movie.model.Movie;
import com.ynet.hmds28.movie.model.MovieQuery;
import com.ynet.hmds28.movie.model.Movieflow;
@Transactional
@HSFProvider(serviceInterface = QueryMovieService.class, serviceVersion = "1.0.0",serviceGroup = "hmds28")
public class QueryMovieServiceImpl implements QueryMovieService {
	@Autowired
	private MovieflowDao movieflowDao;
	@Autowired
	private MovieDao movieDao;
	@Override
	public MovieListBean queryMovie(String serviceConsumeId, String cinemaId) {
		
		//TODO 记录流水,开启GTS事务
		movieflowDao.enableTXC();
		
		MovieListBean movieLists = new MovieListBean();
		
		Movieflow mf=new Movieflow();
		mf.setCalltime(new Date());
		mf.setMovieflowid(UuidUtil.getUUid());
		mf.setServiceconsumeid(serviceConsumeId);
		movieflowDao.insert(mf);
		
		//查询电影
		MovieQuery example=new MovieQuery();
		example.setOrderByClause("lookTime");
		example.createCriteria().andCinemaidEqualTo(cinemaId);
		List<Movie> movies=movieDao.selectByExample(example);
		if(movies == null || movies.size() == 0){
			movieLists.setRemark("此电影院暂无上映电影");
		}else{
			movieLists.setMovieBeans(transfrom(movies));
		}
		
		return movieLists;
	}
	
	public List<MovieBean> transfrom(List<Movie> movies){
		List<MovieBean> movieBeans = new ArrayList<MovieBean>();
		for (Movie movie : movies) {
			MovieBean movieBean = new MovieBean();
			movieBean.setCinemaid(movie.getCinemaid());
			movieBean.setLooktime(movie.getLooktime());
			movieBean.setMoviedesc(movie.getMoviedesc());
			movieBean.setMovieid(movie.getMovieid());
			movieBean.setMovieimg(movie.getMovieimg());
			movieBean.setMoviename(movie.getMoviename());
			movieBeans.add(movieBean);
		}
		return movieBeans;
	}

}
